//package org.ofbiz.lucenetools;
//
//
//import java.nio.file.FileSystems;
//import org.apache.lucene.analysis.Analyzer;
//import org.apache.lucene.analysis.standard.StandardAnalyzer;
//import org.apache.lucene.document.Document;
//import org.apache.lucene.document.Field;
//import org.apache.lucene.document.TextField;
//import org.apache.lucene.index.DirectoryReader;
//import org.apache.lucene.index.IndexWriter;
//import org.apache.lucene.index.IndexWriterConfig;
//import org.apache.lucene.index.Term;
//import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
//import org.apache.lucene.search.BooleanClause;
//import org.apache.lucene.search.IndexSearcher;
//import org.apache.lucene.search.Query;
//import org.apache.lucene.search.ScoreDoc;
//import org.apache.lucene.search.TopDocs;
//import org.apache.lucene.store.Directory;
//import org.apache.lucene.store.FSDirectory;
//
///**
// * 基于Lucene5.5.4
// */
//public class GvSearch
//{
//	public static final String INDEX_PATH = "E:\\lucene-db";
//	
//	/**
//	 * 创建索引
//	 */
//	public void creatIndex()
//	{
//		IndexWriter indexWriter = null;
//		try
//		{
//			Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(INDEX_PATH));
//			Analyzer analyzer = new StandardAnalyzer();
//			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
//			indexWriter = new IndexWriter(directory, indexWriterConfig);
//			//indexWriter.deleteAll();// 清除以前的index
//			
//			
////			Document document = new Document();
////			document.add(new Field("id", "1", TextField.TYPE_STORED));
////			document.add(new Field("title", "android 上海 中国", TextField.TYPE_STORED));
////			document.add(new Field("content", "a1", TextField.TYPE_STORED));
////			document.add(new Field("tag", "a1", TextField.TYPE_STORED));
////			document.add(new Field("url", "a1", TextField.TYPE_STORED));
////			indexWriter.addDocument(document);
////			
////			
////			Document document2 = new Document();
////			document2.add(new Field("id", "2", TextField.TYPE_STORED));
////			document2.add(new Field("title", "android 2 上海中国", TextField.TYPE_STORED));
////			document2.add(new Field("content", "a1", TextField.TYPE_STORED));
////			document2.add(new Field("tag", "a1", TextField.TYPE_STORED));
////			document2.add(new Field("url", "a1", TextField.TYPE_STORED));
////			indexWriter.addDocument(document2);
////			
////			
//			Document document3 = new Document();
//			document3.add(new Field("id", "300", TextField.TYPE_STORED));
//			document3.add(new Field("title", "上海中国", TextField.TYPE_STORED));
//			document3.add(new Field("content", "a1", TextField.TYPE_STORED));
//			document3.add(new Field("tag", "a1", TextField.TYPE_STORED));
//			document3.add(new Field("url", "a1", TextField.TYPE_STORED));
//			indexWriter.addDocument(document3);
//			
//			for(int i=1;i<100;i++) {
//				Document document4 = new Document();
//				document4.add(new Field("id", "1"+i, TextField.TYPE_STORED));
//				document4.add(new Field("title", "android 2 上海中国"+i, TextField.TYPE_STORED));
//				document4.add(new Field("content", "a1"+i, TextField.TYPE_STORED));
//				document4.add(new Field("tag", "a1"+i, TextField.TYPE_STORED));
//				document4.add(new Field("url", "a1"+i, TextField.TYPE_STORED));
//				indexWriter.addDocument(document4);
//
//				  Term id=new Term("id","1"+i);
//	            indexWriter.updateDocument(id,document4);
//			}
//			
//			
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//		finally
//		{
//			try
//			{
//				if(indexWriter != null) indexWriter.close();
//			}
//			catch (Exception e)
//			{
//				e.printStackTrace();
//			}
//		}
//	}
//	
//	/**
//	 * 搜索
//	 */
//	public void search(String keyWord)
//	{
//		DirectoryReader directoryReader = null;
//		try
//		{
//			// 1、创建Directory
//			Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(INDEX_PATH));
//			// 2、创建IndexReader
//			directoryReader = DirectoryReader.open(directory);
//			// 3、根据IndexReader创建IndexSearch
//			IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
//			// 4、创建搜索的Query
//			 Analyzer analyzer = new StandardAnalyzer();
//			//Analyzer analyzer = new IKAnalyzer(true); // 使用IK分词
//			
//			// 简单的查询，创建Query表示搜索域为content包含keyWord的文档
//			//Query query = new QueryParser("content", analyzer).parse(keyWord);
//			
//			String[] fields = {"title", "content", "tag"};
//			// MUST 表示and，MUST_NOT 表示not ，SHOULD表示or
//			BooleanClause.Occur[] clauses = {BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD};
//			// MultiFieldQueryParser表示多个域解析， 同时可以解析含空格的字符串，如果我们搜索"上海 中国" 
//			Query multiFieldQuery = MultiFieldQueryParser.parse(keyWord, fields, clauses, analyzer);
//			
//			// 5、根据searcher搜索并且返回TopDocs
//			TopDocs topDocs = indexSearcher.search(multiFieldQuery, 200); // 搜索前200条结果
//			System.out.println("共找到匹配处：" + topDocs.totalHits);
//			// 6、根据TopDocs获取ScoreDoc对象
//			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//			System.out.println("共找到匹配文档数：" + scoreDocs.length);
//			
////			QueryScorer scorer = new QueryScorer(multiFieldQuery, "content");
////			SimpleHTMLFormatter htmlFormatter = new SimpleHTMLFormatter("<span style=\"backgroud:red\">", "</span>");
////			Highlighter highlighter = new Highlighter(htmlFormatter, scorer);
////			highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer));
//			for (ScoreDoc scoreDoc : scoreDocs)
//			{
//				// 7、根据searcher和ScoreDoc对象获取具体的Document对象
//				Document document = indexSearcher.doc(scoreDoc.doc);
//				String content = document.get("content");
//				//TokenStream tokenStream = new SimpleAnalyzer().tokenStream("content", new StringReader(content));
//				//TokenSources.getTokenStream("content", tvFields, content, analyzer, 100);
//				//TokenStream tokenStream = TokenSources.getAnyTokenStream(indexSearcher.getIndexReader(), scoreDoc.doc, "content", document, analyzer);
//				//System.out.println(highlighter.getBestFragment(tokenStream, content));
//				System.out.println("-----------------------------------------");
//				System.out.println("文章标题："+document.get("title"));
//				System.out.println("文章地址：" + document.get("url"));
//				System.out.println("文章内容：" + content);
////				System.out.println(highlighter.getBestFragment(analyzer, "content", content));
//				System.out.println("");
//				// 8、根据Document对象获取需要的值
//			}
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//		finally
//		{
//			try
//			{
//				if(directoryReader != null) directoryReader.close();
//			}
//			catch (Exception e)
//			{
//				e.printStackTrace();
//			}
//		}
//	}
//
//	public static void main(String args[])
//	{
//		GvSearch demo = new GvSearch();
//		demo.creatIndex();
//		demo.search("上海");
//	}
//}